/**
 * @file field
 * @author fanxueliang@qq.com
 */
import {includes, set, get} from 'lodash-es';
import type {UrlSpecItem, UrlField, ExtensionsAttr} from '../interface';
import {getBasicField} from '../util';

export function createUrlField(spec: UrlSpecItem, showIndex: number, path: string) {
    if (spec.rules) {
        spec.rules.link = true;
    } else {
        spec.rules = {
            link: true
        };
    }
    const extensions: ExtensionsAttr[] = [];
    if (spec?.extraAttr?.extensions) {
        extensions.push(...spec.extraAttr.extensions);
    }

    // url类型默认长度限制1024
    if (get(spec, 'rules.maxLength', null) === null) {
        set(spec, 'rules.maxLength', 2048);
    }

    const basicField = getBasicField(spec, path, showIndex);
    return {
        ...basicField,
        extensions,
        type: 'STRING',
        isLeaf: true,
        spec,
        tip: basicField.tip || 'https://',
        component: 'Input'
    } as UrlField;
}
